package net.orange.architecture.db;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

/**
 * The condition collection using in retrieve a set of object, which include the
 * page division configuration.
 */
public class ListConditions extends Conditions {

	/**
	 * The default page size.
	 */
	private static int defaultPageSize = 10;

	/**
	 * The page size
	 */
	private int pageSize;

	/**
	 * The current page number.
	 */
	private int currPage;

	/**
	 * The record count
	 */
	private long recordCount;

	/**
	 * @return the currPage
	 */
	public int getCurrPage() {
		return currPage > 0 ? currPage : 1;
	}

	/**
	 * @param currPage
	 *            the currPage to set
	 */
	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	/**
	 * @return the pageCount
	 */
	public int getPageCount() {
		return (int) (Math.floor(getRecordCount() / getPageSize()) + ((getRecordCount()
				% getPageSize() == 0) ? 0 : 1));
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize > 0 ? pageSize : defaultPageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the recordCount
	 */
	public long getRecordCount() {
		return recordCount;
	}

	/**
	 * @param recordCount
	 *            the recordCount to set
	 */
	public void setRecordCount(long recordCount) {
		this.recordCount = recordCount;
	}

	/**
	 * Get the all page page number in web page selection.
	 * 
	 * @return the pageNumber
	 */
	public List<SelectItem> getPageNumber() {
		List<SelectItem> list = new ArrayList<SelectItem>();

		for (int i = 1; i <= getPageCount(); ++i) {
			list.add(new SelectItem(new Integer(i), String.valueOf(i)));
		}

		if (list.size() <= 0)
			list.add(new SelectItem(new Integer(1), String.valueOf(1)));

		return list;
	}

}